<?php
class Admin_Model_DAO_Comment
{    
	private $_db;
	private static $_instance = null;
	private static $_table = "comment";
 
	/**
	 * @return Admin_Model_DAO_Comment
	 * */
	public static function factory() {
		if(self::$_instance == null) {
			self::$_instance = new self();
		}
		
		return self::$_instance;
	}
	
    private function __construct()
    {
    	$module = Utils_Global::get('module');
		$this->_db = Utils_Global::getDbInstance($module);
    }
	
    public function insert($data) {
        $result = $this->_db->insert(self::$_table, $data);
        return $result;
    }
    
    public function delete($id) {
        $result = $this->_db->delete(self::$_table, "id={$this->_db->quote($id, 'INTEGER')}");
        return $result;
    }
    
    public function update($id, $data) {
        $result = $this->_db->update(self::$_table, $data, "id={$this->_db->quote($id, 'INTEGER')}");
        return $result;
    }
    
    public function select($options = array()) {
        $sql = " SELECT `comment`.`id`, `course_id`, `t`.`content` `course_name`, `username`, `email`, `comment`, `reply`, `comment`.`dateline`,`comment`.`last_update`,`comment`.`active` ";
        $from = " FROM `comment` INNER JOIN `course` ON `comment`.`course_id` = `course`.`id` 
                    INNER JOIN `translation` t ON `course`.`i18n_title` = `t`.`i18n_id` ";
        $where = " WHERE `t`.`lang` = 'vi' ";
        $order = " ORDER BY `id` DESC ";
        $limit = "";
        if(is_array($options)) {
            if($options['course_id']) {
                $where .= " AND `course_id` = {$this->_db->quote($options['course_id'], 'INTEGER')} ";
            }
            if($options['id']) {
                $where .= " AND `comment`.`id` = {$this->_db->quote($options['id'], 'INTEGER')} ";
            }
            if($options['title']) {
                $where .= " AND `comment`.`comment` LIKE '%" . $options['title'] . "%' ";
            }
            if($options['order'] && $options['by']) {
            	$order = " ORDER BY `comment`.`{$options['order']}` {$options['by']} ";
            }
            if(isset($options['offset']) && $options['offset'] >=0 && $options['limit'] > 0) {
            	$limit = " limit {$this->_db->quote($options['offset'], 'INTEGER')}, {$this->_db->quote($options['limit'], 'INTEGER')} ";
            }
        }
        
        $result = $this->_db->fetchAll($sql . $from . $where . $order . $limit);
        return $result;
    }
    
    public function count($options = array()) {
        $sql = " SELECT count(1) `count` ";
        $from = " FROM `comment` INNER JOIN `course` ON `comment`.`course_id` = `course`.`id` 
                    INNER JOIN `translation` t ON `course`.`i18n_title` = `t`.`i18n_id` ";
        $where = " WHERE `t`.`lang` = 'vi' ";
        if(is_array($options)) {
        	if($options['course_id']) {
        		$where .= " AND `course_id` = {$this->_db->quote($options['course_id'], 'INTEGER')} ";
        	}
        	if($options['id']) {
        		$where .= " AND `comment`.`id` = {$this->_db->quote($options['id'], 'INTEGER')} ";
        	}
        	if($options['title']) {
        		$where .= " AND `comment`.`comment` LIKE '%" . $options['title'] . "%' ";
        	}
        }
        
        $result = $this->_db->fetchOne($sql . $from . $where);
        return $result;
    }
}